(function ($) {
    // 登录/注册
    var login = {
        init: function () {
            var vm = new Vue({
                el: "#loginBox",

                // 数据
                data: {
                    activeType: true,   // 切换登录和注册
                    phone: null,
                    password: null,
                    code: null,
                    codeType: true,     // 获取验证码
                    codeText: '获取验证码', 
                    codeBtnClass: 'code-not-exist',
                    isCheck: true,     // 校验账号密码是否一致
                    centerDialogVisible: false,         // 打开弹窗
                    is_company: 0,
                    is_factory: 0,
                    is_yingpin: 0
                },
                // 周期
                mounted: function () {
                },

                watch: {
                    phone: function (newValue, oldValue) {
                        this._checkUserPhone(oldValue, newValue)
                    }
                },

                computed: {
                    codeTextFun: function () {
                        return this.codeText;
                    }
                },

                // 方法
                methods: {
                    onLogin () {
                        var self = this
                        // 用户登录
                        // 前端先校验一下
                        if (this._checkUserInfo()) {
                            // 前端校验成功
                            console.log("前端校验成功")
                            $.ajax({
                                type:"POST",
                                url: _UrlPrefix + "/api/member/login",
                                data: {
                                    phone: self.phone,
                                    pwd: self.password,
                                    app_access: _AppAccess
                                },
                                success:function(res){
                                    if(200 == res.code || 0 == res.code) {
                                        getWebPop("登录成功", "", 2);
                                        var exp = new Date(); 
                                        exp.setTime(exp.getTime() + 60 * 60 * 1000 * 5); 
                                        document.cookie = "token=" + escape(res.data.token) + ";expires=" + exp.toGMTString() + ";path=/";
                                        document.cookie = "laravel_session=" + escape('WpLd1N5n1dOBgDEaQDp5uY6xIWmqvuBThqdweEZ4') + ";expires=" + exp.toGMTString() + ";path=/";

                                        // 记录token
                                        token = res.data.token;
                                        localStorage.setItem('token', token);

                                        self.centerDialogVisible = true;
                                        self.is_yingpin = res.data.is_yingpin;
                                        self.is_company = res.data.is_company;
                                        self.is_factory = res.data.is_factory;
                                        // 员工页面
                                        // window.location.href = "/pc/public/staff/StaffInfo.html";
                                        // 公司页面
                                        // window.location.href = "/pc/public/company/index.html";
                                        // 工厂页面
                                        // window.location.href = "/pc/public/factory/index.html";
                                    }else if(res.code == 30003) {
                                        self.isCheck = false
                                        setTimeout(function () {
                                            self.isCheck = true
                                        }, 1500)
                                    }
                                },
                                error: function(xhr,status,error) {
                                    // console.log("xhr:", xhr)
                                    // console.log("status:", status)
                                    // console.log("error:", error)
                                },
                                complete: function () {

                                }
                            })
                        } else {
                            // 前端校验失败
                            console.log("前端校验失败")
                            self.isCheck = false
                            setTimeout(function () {
                                self.isCheck = true
                            }, 1500)
                        }
                    },
                    onRegistered () {
                        // 用户注册
                        var self = this
                        if (this._checkUserInfo()) {
                            $.ajax({
                                type:"POST",
                                url: _UrlPrefix + "/api/member/regist",
                                data: {
                                    phone: self.phone,
                                    pwd: self.password,
                                    yzm: self.code,
                                    app_access: _AppAccess
                                },
                                success:function(res){
                                    console.log("注册: ", res)
                                    if(200 == res.code || 0 == res.code) {
                                        alert("注册成功")
                                        console.log("res: ", res)
                                    }else if(res.code == 30002) {
                                        alert("手机号已注册")
                                    }else if(res.code == 30003) {
                                        self.isCheck = false
                                        setTimeout(function () {
                                            self.isCheck = true
                                        }, 1500)
                                    }
                                },
                                error: function(xhr,status,error) {
                                    // console.log("xhr:", xhr)
                                    // console.log("status:", status)
                                    // console.log("error:", error)
                                },
                                complete: function () {

                                }
                            })
                        } else {
                            // 前端校验失败
                            console.log("前端校验失败")
                            self.isCheck = false
                            setTimeout(function () {
                                self.isCheck = true
                            }, 1500)
                        }
                    },
                    onSwitchLogin () {
                        // 切换到登录状态
                        this.activeType = true
                        this.phone = null
                        this.password = null
                        this.code = null
                    },
                    onSwitchRegister () {
                        // 切换到注册状态
                        this.activeType = false
                        this.phone = null
                        this.password = null
                        this.code = null
                        this.codeType = true
                        this.codeText = '获取验证码'
                        this.codeBtnClass = 'code-not-exist'
                        // clearInterval(this.t1)
                    },
                    onGetCode () {
                        // 获取验证码
                        var self = this;
                        if(!this.phone || this.phone.length != 11) {
                            return alert("请输入正确的手机号码");
                        }
                        $.ajax({
                            type:"POST",
                            url: _UrlPrefix + "/api/member/yzm",
                            data: {
                                phone: self.phone,
                                app_access: _AppAccess
                            },
                            success:function(res){
                                if(res.code == 1) {
                                    if (self.codeType) {
                                        self.codeType = false
                                        self.codeBtnClass = 'code-exist'
                                        self._second = 60
                                        self.codeText = self._second + "s后重置"
                                        // 设置倒计时
                                        self.t1 = setInterval(function () {
                                            if (self._second > 0) {
                                                self._second--;
                                                self.codeText = self._second + "s后重置"
                                            } else {
                                                self.codeType = true;
                                                self.codeBtnClass = 'code-not-exist'
                                                self.codeText = "获取验证码"
                                                clearInterval(self.t1)
                                            }
                                        }, 1000)
                                    }
                                }
                            },
                            error: function(xhr,status,error) {
                                // console.log("xhr:", xhr)
                                // console.log("status:", status)
                                // console.log("error:", error)
                            },
                            complete: function () {

                            }
                        })
                        
                    },
                    _checkUserInfo () {
                        if(this.phone && this.phone.length == 11 && this.password && this.password.length > 0) {
                            return true;
                        }
                        return false;
                    },
                    _checkUserPhone (oldValue, newValue) {
                        if(!newValue || newValue == null) return
                        if(newValue.length > 11) {
                            this.phone = oldValue
                            return
                        }
                        for(var i = 0;i < newValue.length;i++) {
                            if(newValue[i] < '0' || newValue[i] > '9') {
                                this.phone = oldValue
                            }
                        }                 
                    },
                    onJumpLink(item) {
                        console.log(item)
                        // 跳转到哪个渠道
                        if('staff' == item) {
                            window.location.href = "/pc/public/staff/StaffInfo.html";
                        }else if('company' == item) {
                            window.location.href = "/pc/public/company/index.html";
                        }else if('factory' == item) {
                            window.location.href = "/pc/public/factory/index.html";
                        } 
                    }
                },

                // 周期
                created: function () {
                },
            })
        }
    }
    login.init()
})(jQuery)